Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
The undefsafe package is a utility for safely accessing deeply nested properties of an object. It allows developers to query properties without worrying about intermediate properties being undefined or null. This can be particularly useful in situations where the structure of an object might be unpredictable or when dealing with JSON objects returned from an API.
Safe property access
This feature allows for the safe querying of nested properties. If any property in the path is undefined, undefsafe will return `undefined` instead of throwing an error.
const undefsafe = require('undefsafe');
const obj = { a: { b: { c: 'd' } } };
console.log(undefsafe(obj, 'a.b.c')); // 'd'
console.log(undefsafe(obj, 'a.x.c')); // undefined
Setting deeply nested values safely
Undefsafe can also be used to set a value at a deeply nested path within an object. If any part of the path does not exist, it will be created.
const undefsafe = require('undefsafe');
let obj = {};
undefsafe(obj, 'a.b.c', 'd');
console.log(obj.a.b.c); // 'd'
Lodash's get function offers similar functionality for safely accessing nested object properties. Unlike undefsafe, lodash.get is part of the larger lodash library, which provides a wide range of utilities for working with arrays, numbers, objects, strings, etc. Lodash.get does not support setting values.
Dot-prop is another package that allows for getting and setting properties on objects using dot-path notation. It supports both getting and setting values, similar to undefsafe. However, dot-prop offers additional features such as deleting properties and checking if an object has a certain property.
Simple function for retrieving deep object properties without getting "Cannot read property 'X' of undefined"
Can also be used to safely set deep values.
var object = {
a: {
b: {
c: 1,
d: [1,2,3],
e: 'remy'
}
}
};
console.log(undefsafe(object, 'a.b.e')); // "remy"
console.log(undefsafe(object, 'a.b.not.found')); // undefined
Demo: https://jsbin.com/eroqame/3/edit?js,console
var object = {
a: {
b: [1,2,3]
}
};
// modified object
var res = undefsafe(object, 'a.b.0', 10);
console.log(object); // { a: { b: [10, 2, 3] } }
console.log(res); // 1 - previous value
As of 1.2.0, undefsafe
supports a *
in the path if you want to search all of the properties (or array elements) for a particular element.
The function will only return a single result, either the 3rd argument validation value, or the first positive match. For example, the following github data:
const githubData = {
commits: [{
modified: [
"one",
"two"
]
}, /* ... */ ]
};
// first modified file found in the first commit
console.log(undefsafe(githubData, 'commits.*.modified.0'));
// returns `two` or undefined if not found
console.log(undefsafe(githubData, 'commits.*.modified.*', 'two'));
FAQs
Undefined safe way of extracting object properties
The npm package undefsafe receives a total of 5,737,610 weekly downloads. As such, undefsafe popularity was classified as popular.
We found that undefsafe demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.